package com.spring.service.impl;

import com.spring.pojo.User;
import com.spring.respone.ApiRestResponse;
import com.spring.service.LoginService;
import com.spring.service.dao.UserDao;

import com.spring.until.PassWordsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    public UserDao userDao ;

    @Override
    public ApiRestResponse login(HttpServletRequest request) {
        String userPass = request.getParameter("userPass") ;
        String password = PassWordsUtil.toMd5(request.getParameter("password"));
        User user = null ;
        if(userPass.length()==13){
            //学号
            user = userDao.selectByStdId(userPass) ;
        }else if(userPass.length()==11){
            //手机
            user = userDao.selectByPhone(userPass) ;
        }else{
            user = userDao.selectByEmail(userPass) ;
        }
        if(user!=null){
            //String pwd = PassWordsUntil.toMd5(password) ;
            if(password.equals(user.getPassword())){
                request.getSession().setAttribute("user",user);
                //密码正确
                return ApiRestResponse.success().setStatus(1) ;
            }else{
                //密码错误
                return ApiRestResponse.success().setStatus(0).setMsg("密码错误");
            }
        }
        //没有这个人
        return ApiRestResponse.error().setStatus(0).setMsg("没这个人") ;
    }

    @Override
    public String loginCheck(HttpServletRequest request) {
        Object o = request.getSession().getAttribute("user") ;
        if(o==null){
            return "redirect:/index" ;
        }else{
            return "spring/LoginSuccess" ;
        }
    }

}
